package jinyilw.swing.rlaf.ui;

import jinyilw.swing.FontTools;
import jinyilw.swing.rlaf.theme.RThemeColor;
import jinyilw.swing.rlaf.utils.RBorderFactory.EmptyBorder;

import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JList;
import javax.swing.border.Border;
import javax.swing.filechooser.FileFilter;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.metal.MetalFileChooserUI;
import java.awt.Component;

/**
 * @author Jinyi
 */
public class RFileChooserUI extends MetalFileChooserUI
{
	private static final Border border = new EmptyBorder(2, 3, 2, 2);

	public static ComponentUI createUI(JComponent c)
	{
		return new RFileChooserUI((JFileChooser) c);
	}

	public RFileChooserUI(JFileChooser fileChooser)
	{
		super(fileChooser);
	}

	@Override
	protected FilterComboBoxRenderer createFilterComboBoxRenderer()
	{
		return new RFilterComboBoxRenderer();
	}

	class RFilterComboBoxRenderer extends FilterComboBoxRenderer
	{
		private static final long serialVersionUID = -6740819401547171567L;

		public RFilterComboBoxRenderer()
		{
			setFont(FontTools.defaultFont);
			setBorder(border);
		}

		@Override
		public Component getListCellRendererComponent(JList list, Object value,
				int index, boolean isSelected, boolean cellHasFocus)
		{
			if (isSelected)
			{
				setOpaque(true);
				setForeground(RThemeColor.selectedTextColor);
				setBackground(RThemeColor.darkThemeColor);
			} else
			{
				setOpaque(false);
				setForeground(RThemeColor.colorSet.textColor);
			}

			if (value instanceof FileFilter)
			{
				setText(((FileFilter) value).getDescription());
			} else
			{
				setText((value == null) ? "" : value.toString());
			}

			return this;
		}
	}
}
